package cn.dglydrpy.fundmanagement.service.impl;

import cn.dglydrpy.fundmanagement.dao.entiey.InvestmentDiary;
import cn.dglydrpy.fundmanagement.dao.mapper.InvestmentDiaryMapper;
import cn.dglydrpy.fundmanagement.service.IInvestmentDiaryService;
import cn.dglydrpy.fundmanagement.service.dto.InvestmentDiaryDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InvestmentDiaryService implements IInvestmentDiaryService {

    @Autowired
    private InvestmentDiaryMapper investmentDiaryMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int saveInvestmentDiary(InvestmentDiaryDto investmentDiaryDto) {
        InvestmentDiary investmentDiary = new InvestmentDiary();
        BeanUtils.copyProperties(investmentDiaryDto, investmentDiary);
        investmentDiary.setCreatedTime(sdf.format(new Date()));
        investmentDiary.setUpdatedTime(sdf.format(new Date()));
        if (investmentDiary.getStatus() == null || investmentDiary.getStatus().isEmpty()) {
            investmentDiary.setStatus("1"); // 1-有效 0-无效
        }
        return investmentDiaryMapper.insert(investmentDiary);
    }

    @Override
    public void updateInvestmentDiary(InvestmentDiaryDto investmentDiaryDto) {
        InvestmentDiary investmentDiary = new InvestmentDiary();
        BeanUtils.copyProperties(investmentDiaryDto, investmentDiary);
        investmentDiary.setUpdatedTime(sdf.format(new Date()));
        investmentDiaryMapper.updateById(investmentDiary);
    }

    @Override
    public void deleteInvestmentDiary(Long id) {
        investmentDiaryMapper.deleteById(id);
    }

    @Override
    public InvestmentDiaryDto getInvestmentDiaryById(Long id) {
        InvestmentDiary investmentDiary = investmentDiaryMapper.selectById(id);
        if (investmentDiary == null) {
            return null;
        }
        InvestmentDiaryDto investmentDiaryDto = new InvestmentDiaryDto();
        BeanUtils.copyProperties(investmentDiary, investmentDiaryDto);
        setCategoryName(investmentDiaryDto);
        return investmentDiaryDto;
    }

    @Override
    public Page<InvestmentDiaryDto> getInvestmentDiarysByConditions(String fundCode, String portfolioId, String category, int pageNo, int pageSize) {
        Page<InvestmentDiary> page = new Page<>(pageNo, pageSize);
        QueryWrapper<InvestmentDiary> queryWrapper = new QueryWrapper<>();
        if (fundCode != null && !fundCode.isEmpty()) {
            queryWrapper.like("fund_code", fundCode);
        }
        if (portfolioId != null && !portfolioId.isEmpty()) {
            queryWrapper.eq("portfolio_id", portfolioId);
        }
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq("category", category);
        }
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        investmentDiaryMapper.selectPage(page, queryWrapper);

        Page<InvestmentDiaryDto> dtoPage = new Page<>(pageNo, pageSize, page.getTotal());
        List<InvestmentDiaryDto> records = page.getRecords().stream().map(investmentDiary -> {
            InvestmentDiaryDto dto = new InvestmentDiaryDto();
            BeanUtils.copyProperties(investmentDiary, dto);
            setCategoryName(dto);
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(records);
        return dtoPage;
    }

    @Override
    public List<InvestmentDiaryDto> getAllInvestmentDiarys() {
        QueryWrapper<InvestmentDiary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        List<InvestmentDiary> investmentDiarys = investmentDiaryMapper.selectList(queryWrapper);
        return investmentDiarys.stream().map(investmentDiary -> {
            InvestmentDiaryDto dto = new InvestmentDiaryDto();
            BeanUtils.copyProperties(investmentDiary, dto);
            setCategoryName(dto);
            return dto;
        }).collect(Collectors.toList());
    }

    private void setCategoryName(InvestmentDiaryDto dto) {
        if ("1".equals(dto.getCategory())) {
            dto.setCategoryName("投资决策");
        } else if ("2".equals(dto.getCategory())) {
            dto.setCategoryName("市场分析");
        } else if ("3".equals(dto.getCategory())) {
            dto.setCategoryName("经验总结");
        } else if ("4".equals(dto.getCategory())) {
            dto.setCategoryName("其他");
        }
    }
}